package com.haixiaoke.saas.order.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.haixiaoke.saas.common.utils.timePair.TimePair;
import com.haixiaoke.saas.common.utils.timePair.TimePairCompareUtil;
import com.haixiaoke.saas.order.domain.OrderLock;
import com.haixiaoke.saas.order.mapper.OrderLockMapper;
import com.haixiaoke.saas.order.service.IOrderLockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单锁单Service业务层处理
 *
 * @author panda
 * @date 2024-02-01
 */
@Service
public class OrderLockServiceImpl implements IOrderLockService {
    @Autowired
    private OrderLockMapper orderLockMapper;

    /**
     * 查询订单锁单
     *
     * @param lockId 订单锁单主键
     * @return 订单锁单
     */
    @Override
    public OrderLock selectOrderLockByLockId(Long lockId) {
        return orderLockMapper.selectOrderLockByLockId(lockId);
    }

    /**
     * 查询订单锁单列表
     *
     * @param orderLock 订单锁单
     * @return 订单锁单
     */
    @Override
    public List<OrderLock> selectOrderLockList(OrderLock orderLock) {
        return orderLockMapper.selectOrderLockList(orderLock);
    }

    /**
     * 新增订单锁单
     *
     * @param orderLock 订单锁单
     * @return 结果
     */
    @Override
    public int insertOrderLock(OrderLock orderLock) {
        return orderLockMapper.insertOrderLock(orderLock);
    }

    /**
     * 修改订单锁单
     *
     * @param orderLock 订单锁单
     * @return 结果
     */
    @Override
    public int updateOrderLock(OrderLock orderLock) {
        return orderLockMapper.updateOrderLock(orderLock);
    }

    /**
     * 批量删除订单锁单
     *
     * @param lockIds 需要删除的订单锁单主键
     * @return 结果
     */
    @Override
    public int deleteOrderLockByLockIds(Long[] lockIds) {
        return orderLockMapper.deleteOrderLockByLockIds(lockIds);
    }

    /**
     * 删除订单锁单信息
     *
     * @param lockId 订单锁单主键
     * @return 结果
     */
    @Override
    public int deleteOrderLockByLockId(Long lockId) {
        return orderLockMapper.deleteOrderLockByLockId(lockId);
    }

    /**
     * 校验订单在该时间段内是否可以下单
     *
     * @param checkInTime  入住时间
     * @param checkOutTime 退房时间
     * @param houseId      房屋id
     * @param orderId      订单id
     * @return true为不可以下单
     */
    @Override
    public Boolean checkHouseLock(Date checkInTime, Date checkOutTime, String houseId, String orderId, Boolean isStrict) {
        boolean result = false;
        List<OrderLock> orderLocks = orderLockMapper.selectOrderLockDateList(checkInTime, houseId, orderId);
        if (CollectionUtil.isNotEmpty(orderLocks)) {
            //封装对象list
            List<TimePair> timePairs = orderLocks.stream().map(x ->
                    new TimePair(x.getCheckInTime().getTime(), x.getCheckOutTime().getTime())
            ).collect(Collectors.toList());

            //将当前欲订单的时间段加入集合比较
            TimePair timePair = new TimePair(checkInTime.getTime(), checkOutTime.getTime());
            timePairs.add(timePair);

            result = TimePairCompareUtil.isOverlap(timePairs, isStrict);
        }
        return result;
    }

    @Override
    public int deleteOrderLockByOrderId(String orderId) {
        return orderLockMapper.deleteOrderLockByOrderId(orderId);
    }

    @Override
    public List<OrderLock> selectOrderLockListByHouseId(String houseId, DateTime date) {
        return orderLockMapper.selectOrderLockListByHouseId(houseId,date);
    }

    @Override
    public int updateOrderLockByOrderId(OrderLock lock) {
        return orderLockMapper.updateOrderLockByOrderId(lock);
    }

    @Override
    public OrderLock selectOrderLockByOrderId(String orderId) {
        return orderLockMapper.selectOrderLockByOrderId(orderId);
    }
}
